﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IFViewDetail.KuaiQi.Others;
using ZedGraph;
using IFClientDLL;
using IFClientDLL.MyCalc.Data.Candle;

namespace FrmTryIF.Ctl.Calc.CtlDataImage
{
    public partial class CtlCandle : CtlZedGraphBase
    {
        public CtlCandle()
        {
            InitializeComponent();
        }
        public override void InitTryIF(ITryIF tryIF)
        {
            base.InitTryIF(tryIF);
            this.Text = "闪电图";
            InitChartGrap(zedGraphControl1.GraphPane);
            tryIF.Calc.DataManager.OnCandleChanged_Minute += new Action<MyData_Candle_Minute>(DataManager_OnCandleChanged_Minute);
        }
        private bool _IsFirstData = false;
        void DataManager_OnCandleChanged_Minute(MyData_Candle_Minute obj)
        {
            var dt = this.CalcXDataByDateTime(obj.MinuteTime);
            if (_IsFirstData == false)
            {
                StockPt fpt = new StockPt();
                fpt.Close = (double)obj.ClosePrice;
                fpt.Date = dt;
                fpt.High = (double)obj.HighPrice;
                fpt.Low = (double)obj.LowPrice;
                fpt.Open = (double)obj.OpenPrice;
                fpt.Vol = (double)obj.TradeVolume;

                fpt.X = this.CalcXDataByDateTime(obj.MinuteTime.AddMinutes(-1));
                _Candle.AddPoint(fpt);
                zedGraphControl1.GraphPane.YAxis.Scale.Max = (double)obj.ClosePrice + _YStep;
                zedGraphControl1.GraphPane.YAxis.Scale.Min = (double)obj.ClosePrice - _YStep;
                _IsFirstData = true;
            }
            StockPt pt =null;
            if (_Candle.Points.Count != 0 && _Candle.Points[_Candle.Points.Count - 1].X == dt)
            {
                pt = _Candle.Points[_Candle.Points.Count - 1] as StockPt;

            }
            else
            {

                pt = new StockPt();
                
            }
           
            pt.Close = (double)obj.ClosePrice;
            pt.Date = dt;
            pt.High = (double)obj.HighPrice;
            pt.Low = (double)obj.LowPrice;
            pt.Open = (double)obj.OpenPrice;
            pt.Vol = (double)obj.TradeVolume;
            pt.X = dt;
            if (_Candle.Points.Count != 0 && _Candle.Points[_Candle.Points.Count - 1].X == dt)
            {
               
            }
            else
            {
                _Candle.AddPoint(pt);
            }
            SetYScale(dt);
            
            
        }
        private void SetYScale(double dt)
        {
            double min = 10000;
            double max = -10000;
            if (dt > 400)
            {

                double mindt = this.CalcXDataByDateTime(XDate.XLDateToDateTime(dt).AddMinutes(-1));
                //zedGraphControl1.GraphPane.XAxis.Scale.Max = dt;
                //zedGraphControl1.GraphPane.XAxis.Scale.Min = mindt;

                for (int i = _Candle.Points.Count - 1; i >= 0; i--)
                {

                    if (_Candle.Points[i].Y > max)
                    {
                        max = _Candle.Points[i].Y;
                    }
                    if (_Candle.Points[i].Y < min)
                    {
                        min = _Candle.Points[i].Y;
                    }
                }
               
            }




            if (max > zedGraphControl1.GraphPane.YAxis.Scale.Max || min < zedGraphControl1.GraphPane.YAxis.Scale.Min)
            {
                zedGraphControl1.GraphPane.YAxis.Scale.Max += _YStep;
                zedGraphControl1.GraphPane.YAxis.Scale.Min -= _YStep;
            }
            try
            {
                zedGraphControl1.AxisChange();
                zedGraphControl1.Invalidate();
            }
            catch
            {

            }




        }
        JapaneseCandleStickItem _Candle;
        private void InitChartGrap(GraphPane grapPane)
        {
            grapPane.Fill = new ZedGraph.Fill(Color.Black);
            grapPane.Chart.Fill = new ZedGraph.Fill(Color.Black);
            grapPane.Chart.Border.Color = Color.Red;
            grapPane.XAxis.Type = AxisType.Date;
            grapPane.XAxis.Scale.Format = "t";
            grapPane.YAxis.MajorGrid.IsVisible = true;
            grapPane.XAxis.MajorGrid.IsVisible = true;
            //grapPane.YAxis.MinorGrid.IsVisible = true;
            grapPane.XAxis.Scale.IsSkipLastLabel = false;
            grapPane.XAxis.Scale.IsSkipFirstLabel = false;
            grapPane.YAxis.Scale.IsSkipFirstLabel = false;
            grapPane.YAxis.Scale.IsSkipLastLabel = false;
            grapPane.YAxis.Scale.FontSpec.Size = 9;
            grapPane.XAxis.Scale.FontSpec.Size = 9;
            grapPane.Y2Axis.Scale.FontSpec.Size = 9;
            grapPane.YAxis.Scale.Format = "n1";
            grapPane.YAxis.Scale.MagAuto = false;
            //grapPane.XAxis.Scale.MagAuto = false;
            grapPane.Y2Axis.Scale.MagAuto = false;
            grapPane.Legend.IsVisible = false;
            grapPane.XAxis.Title.IsVisible = false;
            grapPane.YAxis.Title.IsVisible = false;
            grapPane.XAxis.MajorGrid.Color = Color.Red;
            grapPane.XAxis.MajorTic.Color = Color.Red;
            grapPane.YAxis.MajorGrid.Color = Color.Red;
            grapPane.YAxis.MajorTic.Color = Color.Red;
            grapPane.XAxis.Scale.FontSpec.FontColor = Color.Red;
            grapPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
            grapPane.YAxis.Scale.MaxAuto = false;
            grapPane.YAxis.Scale.MinAuto = false;


            
            _Candle = grapPane.AddJapaneseCandleStick("price", new StockPointList());

            _Candle.Stick.IsAutoSize = true;
            _Candle.Stick.Color = Color.Red;
            _Candle.Stick.FallingColor = Color.WhiteSmoke;
            _Candle.Stick.FallingBorder.Color = Color.WhiteSmoke;
            _Candle.Stick.FallingFill.Color = Color.WhiteSmoke;
            _Candle.Stick.RisingBorder.Color = Color.Red;
            _Candle.Stick.RisingFill.Color = Color.Red;
            _Candle.Stick.IsOpenCloseVisible = true;
            SetXAxis(zedGraphControl1);
            SetXAxisFormat(zedGraphControl1);
        }

      

       
    }
}
